if(!recordManage){
	var recordManage = {};
}

$.extend(recordManage,{
	from:''
	,recordList:[]
	,sourceList:[]
	,errorTypeMap:{}
	,urlMap:{
		 searchRecord:"../record/list.do"
		,createRecord:"../record/create.do"
		,updateRecord:"../record/update.do"
		,deleteRecord:"../record/delete.do"
		,exportCsv:"../record/exportCsv.do"
		,listSourceAndErrorType:"../recordSource/makeDbSelect.do"
		,listDealIedas:"../dealIdea/list.do"
	}
	,trTemplate:'<tr><td>{receieveDateStr}</td><td>{submitPerson}</td><td>{source.name}</td><td>{errorType.name}</td><td>{company}</td>'
		+'<td><a href="javascript:void(0)" onclick="recordManage.showDesc({INDEX})"><div style="width:100%">{shortDesc}</div></a></td><td>{fmtIsBad}</td><td>{fmtStatus}</td>'
		+'<td nowrap="nowrap"><a href="javascript:void(0)" onclick="recordManage.editRecord({INDEX})"><li class="ui-state-default ui-corner-all" title="编辑"><span class="ui-icon ui-icon-wrench"></span></li></a>'
		+'<a href="javascript:void(0)" onclick="recordManage.delRecord({INDEX})"><li class="ui-state-default ui-corner-all" title="删除"><span class="ui-icon ui-icon-trash"></span></li></a></td></tr>'
	,sourceTemplate:'<input type="radio" id="iSource{INDEX}" name="iSourceRadio" value="{id}"/><label for="iSource{INDEX}">{name}</label>'
	,editSourceTemp:'<input type="radio" id="uSource{INDEX}" name="uSourceRadio" value="{id}"/><label for="uSource{INDEX}">{name}</label>'
	,search:function(type){
		var msg="",params={};
		if(type=="add"){
			var today = dateUtil.formatDate(new Date(),"yyyy-mm-dd");
			params.beginDate = today;
			params.endDate = today;
			msg = "今日已录入 ";
		}else{
			params.schCompany = commonUtil.getLikeTextVal("#schCompany");
			params.schSubmitPerson = commonUtil.getLikeTextVal("#schSubmitPerson");
			params.beginDate = $("#sBeginDate").val();
			params.endDate = $("#sEndDate").val();
			params.schStatus = $(":radio:checked[name=sState]").val();
			params.schIsBad = $(":radio:checked[name=schIsBad]").val();
			msg = "搜索结果共 ";
		}
		commonUtil.jsonPost(recordManage.urlMap["searchRecord"],params,function(data){
			recordManage.from = type;
			recordManage.recordList=data.pojoList;
			commonUtil.generateDataTable(data.pojoList,"databody_record",recordManage.trTemplate);
			$("#countp_record").html(msg + data.pojoList.length + " 条");
		},"#databody_record,#countp_record");
	}
	,exportCsv:function(){
		var params={};
		params.schCompany = commonUtil.getLikeTextVal("#schCompany");
		params.schSubmitPerson = commonUtil.getLikeTextVal("#schSubmitPerson");
		params.beginDate = $("#sBeginDate").val();
		params.endDate = $("#sEndDate").val();
		params.schStatus = $(":radio:checked[name=sState]").val();
		params.schIsBad = $(":radio:checked[name=schIsBad]").val();
		var pa = $.param(params);
		window.open(recordManage.urlMap["exportCsv"]+"?"+pa);
	}
	,sourceChange:function(sourceRadio,errorTypeId){
		var sourceId = $(":radio[name="+sourceRadio+"]:checked").val();
		$("#"+errorTypeId).empty().append("<option value=''>--请选择--</option>");
		$('#'+errorTypeId+'~span>input').val('');
		$.each(recordManage.errorTypeMap[sourceId],function(i,n){
			$("#"+errorTypeId).append("<option value='"+n.id+"'>"+n.name+"</option>");
		});
	}
	,saveRecord:function(){
		var params = commonUtil.getInputValues("#recordForm");
		if(params["errorTypeId"]=="" || params["errorTypeId"]==undefined){
			commonUtil.showAlert("错误","故障类型必须选择一个。");
		}else{
			params["pojo.source.id"]=params["iSourceRadio"];
			params["pojo.errorType.id"]=params["errorTypeId"];
			params["pojo.isBad"]=params["iIsBadRadio"];
			params["pojo.status"]=params["iStateRadio"];
			commonUtil.jsonPost(recordManage.urlMap["createRecord"],params,function(data){
				commonUtil.showAlert("完成","添加成功");
				recordManage.resetForm();
				recordManage.search("add");
			});
		}
	}
	,resetForm:function(){
		$("#recordForm").get(0).reset();
		recordManage.sourceChange("iSourceRadio","errorTypeId");
		$("#errorTypeId").val('');
	}
	,showDesc:function(i){
		$('#searchDescDetail').html(recordManage.recordList[i].problemDesc);
		$('#searchDescDiv').dialog({width:500,height:400,modal:false,buttons: {
			"关闭":function(){ $(this).dialog("close"); }
		}});
	}
	,delRecord:function(i){
		commonUtil.showConfirm("确认","确定删除这条工单？",{"确定":function(){
			commonUtil.jsonPost(recordManage.urlMap["deleteRecord"],{"pojo.id":recordManage.recordList[i].id},function(data){
				$(commonUtil.confirmId).dialog("close");
				commonUtil.showAlert("完成","删除成功");
				recordManage.search(recordManage.from);
			});
		}});
	}
	,editRecord:function(i){
		$('#editDiv').dialog({ show: "fade", modal: true, width:"85%", height:"600", buttons: {
			"保存":function(){
				var params = commonUtil.getInputValues("#editDiv");
				if(params["uerrorTypeId"]=="" || params["uerrorTypeId"]==undefined){
					commonUtil.showAlert("错误","故障类型必须选择一个。");
				}else{
					params["pojo.source.id"]=params["uSourceRadio"];
					params["pojo.errorType.id"]=params["uerrorTypeId"];
					params["pojo.isBad"]=params["uIsBadRadio"];
					params["pojo.status"]=params["uStateRadio"];
					commonUtil.jsonPost(recordManage.urlMap["updateRecord"],params,function(data){
						commonUtil.showAlert("完成","修改成功");
						recordManage.search(recordManage.from);
						$('#editDiv').dialog("close");
					});
				}
			}
			,"取消":function(){ $(this).dialog("close"); } 
		}});
		commonUtil.setInputValues("#editDiv",{},"pojo.");
		var record = recordManage.recordList[i];
		commonUtil.setInputValues("#editDiv",record,"pojo.");
		commonUtil.generateDataTable(recordManage.sourceList,"uSource",recordManage.editSourceTemp);
		$(":radio[name=uSourceRadio]").click(function(){recordManage.sourceChange("uSourceRadio","uerrorTypeId");});
		$.each($(":radio[name=uSourceRadio]"),function(i,n){
			if(n.value==record.source.id){
				$(n).attr("checked","checked");
				return;
			}
		});
		$("#uErrorTypeTd").empty().html('<select id="uerrorTypeId" name="uerrorTypeId"></select>');
		recordManage.sourceChange("uSourceRadio","uerrorTypeId");
		$("#uerrorTypeId").val(record.errorType.id).combobox();
		$("#uSource").buttonset();
		$("#uIsBad").empty().html('<input type="radio" id="uIsBad1" name="uIsBadRadio" value="0"/><label for="uIsBad1">合理</label><input type="radio" id="uIsBad2" name="uIsBadRadio" value="1"/><label for="uIsBad2">不合理</label>');
		$("#uState").empty().html('<input type="radio" id="uState1" name="uStateRadio" value="1"/><label for="uState1">处理中</label><input type="radio" id="uState2" name="uStateRadio" value="3"/><label for="uState2">处理完毕</label><input type="radio" id="uState3" name="uStateRadio" value="4"/><label for="uState3">疑难不能解决</label>');
		$(":radio[name=uIsBadRadio][value="+record.isBad+"]").attr("checked","checked");
		$(":radio[name=uStateRadio][value="+record.status+"]").attr("checked","checked");
		$("#uIsBad").buttonset();
		$("#uState").buttonset();
		$("#editDiv textarea[name=dealIdea]").val('');
		commonUtil.jsonPost(recordManage.urlMap["listDealIedas"],{"recordId":record.id},function(data){
			commonUtil.generateDataTable(data.pojoList,"ideaBody","<tr><td>{ideaDateStr}</td><td>{idea}</td></tr>");
			$("#countIdeas").html(" 共 " + data.pojoList.length +" 条。");
		},"#ideaBody");
		$("#dealIdea_table").accordion({collapsible:true,active:2});
	}
	,sourceMake:function(){
		commonUtil.jsonPost(recordManage.urlMap["listSourceAndErrorType"],{},function(data){
			recordManage.errorTypeMap=data.errorTypeMap;
			recordManage.sourceList=data.pojoList;
			
			commonUtil.generateDataTable(data.pojoList,"iSource",recordManage.sourceTemplate);
			$(":radio[name=iSourceRadio]:first").attr("checked","checked");
			$("#iSource").buttonset().find(":radio").click(function(){recordManage.sourceChange("iSourceRadio","errorTypeId");});
			recordManage.sourceChange("iSourceRadio","errorTypeId");
			
			commonUtil.generateDataTable(data.pojoList,"uSource",recordManage.editSourceTemp);
			$("#uSource").click(function(){recordSearch.sourceChange("uSourceRadio","uerrorTypeId");});
		},"#iSource,#uSource");
	}
});

$(document).ready(function(){
	$("#record_body th,#dealIdea_table th").addClass("ui-widget-header");
	$("#record_body :text").attr("class","text ui-widget ui-widget-content ui-corner-all");
	$("#editDiv :text,textarea").attr("class","text ui-widget ui-widget-content ui-corner-all");
	
	$("#insert_submit").button().click(recordManage.saveRecord);
	$("#insert_reset").button().click(recordManage.resetForm);
	$("#searchBt").button().click(recordManage.search);
	$("#csvBt").button().click(recordManage.exportCsv);
	
	$("#record_accordion").accordion({collapsible:true,active:1});
	$("#list_table").accordion({collapsible:true,active:0});
	$("#iIsBad,#iState,#searchIsBad,#searchState").buttonset();
	$("#errorTypeId").combobox();
	
	$("#sBeginDate").datepicker();
	$("#sBeginDate").datepicker("setDate",'+0d');
	$("#sEndDate").datepicker();
	$("#sEndDate").datepicker("setDate",'+0d');
	commonUtil.likeText("#schCompany,#schSubmitPerson");
	
	window.setInterval(function(){$("#nowTime").html(dateUtil.formatDate(new Date(),"yyyy-mm-dd hh:nn:ss"));},1000);

	recordManage.sourceMake();
	
	recordManage.search("add");
});
